﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Web.Security;

namespace IMPPFront
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void Send_Login(object sender, DirectEventArgs e)
        {
                if (string.IsNullOrEmpty(txtPassword.Text) || string.IsNullOrEmpty(txtUsername.Text))
                {
                    X.Msg.Notify("Waarschuwing!", "Vul A.U.B. beide velden in en probeer opnieuw.").Show();
                }
                else
                {
                    bool loginCorrect = Membership.ValidateUser(txtUsername.Text, txtPassword.Text);
                    if (loginCorrect)
                    {
                        if (Request.QueryString["ReturnUrl"] != null)
                          {
                              FormsAuthentication.SetAuthCookie(txtUsername.Text, false);
                              Response.Redirect(Request.QueryString["ReturnUrl"]);
                          }
                          else
                          {
                            FormsAuthentication.SetAuthCookie(txtUsername.Text, false);
                            MembershipUser user = Membership.GetUser(txtUsername.Text);
                            if(Roles.IsUserInRole(user.UserName, "managers"))
                            {
                                Response.Redirect("~/Management/Persons.aspx", true);
                            } else if(Roles.IsUserInRole(user.UserName, "employees"))
                            {
                                Response.Redirect("~/Employees/PP.aspx", true);
                            }
                          }
                    }
                    else
                    {
                        X.Msg.Notify("Mislukt!", "Geen geldige gegevens gevonden voor ingegeven waardes. Probeer A.U.B. opnieuw.").Show();
                    }
                }
            }
        }
    }

